package com.mainbo.androidframework.utils;

import android.content.Context;
import android.os.Environment;


import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Locale;
import com.mainbo.androidframework.App;

/**
 * Created by lsmtty on 2016/3/2.
 */
public class FileHelper {
    private static Context mContext = App.mContext;
    private static SimpleDateFormat mDateformat = new SimpleDateFormat(
            "yyyy_MM_dd_HH_mm_ss", Locale.getDefault());
    /**
     * 获取SD卡路径
     *
     * @return
     */
    public static String getSDcardPath() {
        String path = "";
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            path = Environment.getExternalStorageDirectory().getPath();
        } else {
            File sdDir = Environment.getExternalStorageDirectory();
            if (sdDir != null && sdDir.exists()) {
                path = Environment.getExternalStorageDirectory().getPath();
            }
        }
        return path + File.separator;
    }
    /**
     * 日志存储根目录，原数据存储根目录.<br>
     * /sdcard/Android/Uplus/
     *
     * @return
     */
    public static File getSDcardAppDir() {
        File appDir = new File(getSDcardPath() + "Uemis");
        if (!appDir.exists()) {
            appDir.mkdirs();
        }
        return appDir;
    }

    /**
     * SD卡上数据存储根目录.<br>
     * /sdcard/Android/data/com.mainbo.uplus/files/
     *
     * @return
     */
    public static File getAppDir() {
        File appDir = null;
        if (Environment.MEDIA_MOUNTED.equals(Environment
                .getExternalStorageState())
                && Environment.getExternalStorageDirectory().exists()) {
            appDir =  mContext.getExternalCacheDir();
        }
        if (appDir == null) {
            appDir = mContext.getFilesDir();
        }
        return appDir;
    }

    /**
     * 工作日志文件目录.
     *
     * @return
     */
    public static File getWorkLogDir() {
        File exceptionLogDir = new File(getAppDir() + "/workLog");
        if (!exceptionLogDir.exists()) {
            exceptionLogDir.mkdirs();
        }
        return exceptionLogDir;
    }

    /**
     * 工作日志文件路径.
     *
     * @return
     */
    public static File getWorkLogFile() throws IOException {
        long time = System.currentTimeMillis();
        File logFile = new File(getWorkLogDir(), mDateformat.format(time)
                + ".log.txt");
        if (!logFile.exists()) {
            logFile.createNewFile();
        }
        return logFile;
    }

    /**
     * 读取当前项目的Assets目录的文件
     *
     * @param fileName
     * @return
     */
    public static String getStrFromAssetsFile(String fileName) {
        InputStream is;
        String str = "";
        try {
            is = App.mContext.getAssets().open(fileName);
            byte[] b = new byte[1024];
            int hasRead;
            while ((hasRead = is.read(b)) > 0) {
                str += new String(b, 0, hasRead, "utf-8");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return str;
    }

    public static String getHtmlBaseUrl() {
        String baseUrl = "file:///" + getSDcardAppDir().getAbsolutePath() + "/";
        return baseUrl;
    }

/*    public static String getTestPictureBookBaseUrl(PictureBook pictureBook) {
        String baseUrl = "file:///" +  new File(getPictureBookDir(pictureBook.id), pictureBook.book.id).getAbsolutePath() + "/";
        return baseUrl;
    }*/

   /* public static File getPictureBookDir(String pictureBookId) {
        return new File(getSDcardAppDir(), "test/" + pictureBookId);
    }
    public static File getTestPictureBookFile(String pictureBookId) {
        return new File(getSDcardAppDir(), "test/" + pictureBookId + "/toc.json");
    }

   *//* public static File getTestPictureBookCover(PictureBook pictureBook) {
        return new File(getPictureBookDir(pictureBook.id), pictureBook.book.coverPath);
    }*//*
    public static String getTestExerciseBaseUrl(String pictureBookId, String exerciseId) {
        String baseUrl = "file:///" + getPictureBookDir(pictureBookId).getAbsolutePath()  + "/" + exerciseId + "/";
        return baseUrl;
    }*/

  /*  public static File getCourseDir() {
        File file = new File(getSDcardAppDir(), "courses");
        if (!file.exists()) {
            file.mkdirs();
        }
        return file;
    }
    public static File getCourseFile(String courseId) {
        return new File(getCourseDir(), courseId);
    }

    public static File getExaminationDir() {
        File file = new File(getSDcardAppDir(), "examination");
        if (!file.exists()) {
            file.mkdirs();
        }
        return file;
    }

    public static File getExaminationFile(String examinationId) {
        return new File(getExaminationDir(), examinationId);
    }

    public static File getCameraFile() {
        return new File(getSDcardAppDir(), "testCamera");
    }*/
}
